/*
 * Copyright 2013 - learnNcode (learnncode@gmail.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.telpoo.bhlib.share.twitter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.Toast;

import com.telpoo.bhlib.R;
import com.telpoo.bhlib.share.twitter.HelperMethods.TwitterCallback;

public class ShareTwitterActivity extends com.telpoo.frame.ui.BaseActivity {
	private Context context;
	String urlPath = "";
	String status = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		context = ShareTwitterActivity.this;

		urlPath = getIntent().getStringExtra("url-picture");
		status = getIntent().getStringExtra("status");
		
		if(urlPath!=null && urlPath.length()>155){
			showToast(R.string.status_qua_gioi_han_twitter);
			finish();
		}

		if (LoginTwitterActivity.isActive(context)) {
			showProgressDialog(ShareTwitterActivity.this);
			try {

				if (urlPath != null)
					HelperMethods.postToTwitterWithImage(context, ((Activity) context), urlPath, status, new TwitterCallback() {

						@Override
						public void onFinsihed(Boolean response) {
							Toast.makeText(context, response ? getString(R.string.success) : getString(R.string.connect_error), Toast.LENGTH_SHORT).show();
							System.out.println("----------------response----------------" + response);
							closeProgressDialog();
							finish();
						}
					});
				else
					HelperMethods.postToTwitter(context, ((Activity) context), status, new TwitterCallback() {

						@Override
						public void onFinsihed(Boolean response) {
							Toast.makeText(context, response ? getString(R.string.success) : getString(R.string.connect_error), Toast.LENGTH_SHORT).show();
							System.out.println("----------------response----------------" + response);
							closeProgressDialog();
							finish();

						}
					});

			} catch (Exception ex) {
				Toast.makeText(context, getString(R.string.connect_error), Toast.LENGTH_SHORT).show();
				ex.printStackTrace();
				closeProgressDialog();
				finish();
			}
		} else {
			startActivityForResult(new Intent(context, LoginTwitterActivity.class), 2213);

		}

	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		finish();
	}
}
